<template>
	<view class="order_box">
		<!-- head区域 -->
		<view class="head">
			<view class="useraddress" >
				<view class="addressinfo" v-if="localBuffer"  hover-class="touchOpacity" @click="goAddress">
					<text space="ensp" class="addressinfo_top">{{address.username}}   {{address.userphone}}</text>
					<view class="addressinfo_bottom">
						<!-- <uni-tag  size="small" type="error" :inverted="true" :circle="true" text="默认"></uni-tag>
						<uni-tag  size="small" type="error" :inverted="true" :circle="true" text="默认"></uni-tag> -->
						<text class="addressinfo_bottom_status" v-if="address.status ==1">默认</text>
						<text class="addressinfo_bottom_label" v-if="address.label != '' ">{{address.label}}</text>
						<text class="addressinfo_bottom_useraddress">{{address.location+address.useraddress}}</text>
					</view>
				</view>
				<view class="addressinfo" v-else>
					<u-button type="warning" :plain="true" @click="addressAdd">+点击完善收货地址</u-button>
				</view>
			</view>
		</view>
		
		<view class="order_ticket">
			<image src="../../static/icon/index/kingkong/selfsupport.png"></image>
			<text>品牌精选</text>
			<text>以免运费 |</text>
			<text>领券</text>
		</view>
		<!-- 商品信息区域 -->
		<view class="order_view">
			<!-- 明日来写购物车基本信息 -->
			<view 
				class="cartInfochild" 
				v-for="(item,index) in orders"
				:key="index"
			>
				<!-- 图片 -->
				<image :src="item.img" class="cartInfo_image"></image>
				<!-- 信息 -->
				<view class="cartInfo_info">
					<text>{{item.goodsname}}</text>
					<text>￥
						<text style="font-size: 40rpx;color:#FF6040;">{{item.price}}</text>
					</text>
					<text >七天无理由退货</text>
				</view>
				<!-- 加减数量 -->
				<view class="cartInfo_num">
					<label> ×{{item.num}}</label>
				</view>
			</view>
		</view>
		
		<!-- 具体详细1 -->
		<view class="resDetail">
			<view hover-class="touchOpacity">
				<label for="" class="resDetail_title">发票类型</label>
				<label for="" style="margin-right:40rpx;color:#333333;font-size: 24rpx;">不开发票 ></label>
			</view>
			<view class="">
				<label for="" class="resDetail_title">售后免邮</label>
				<label for="" style="margin-right:40rpx;color:#666666;font-size: 24rpx;">部分商家赠送</label>
			</view>
			<view class="" >
				<label for="" class="resDetail_title">卖家留言</label>
				<label for="" style="margin-right:40rpx;color:#666666;font-size: 24rpx;">填写内容需要与商家协商并确认，45字以内</label>
			</view>
		</view>
		
		<!-- 具体详细2 -->
		<view class="resDetail" style="margin:10rpx 0 98rpx;">
			<view class="">
				<label for="" class="resDetail_title">商品金额</label>
				<label for="" style="margin-right:40rpx;color:#333333;font-size: 24rpx;">￥{{countmoney}}</label>
			</view>
			<view class="">
				<label for="" class="resDetail_title">优惠活动</label>
				<label for="" style="margin-right:40rpx;color:#FF6040;font-size: 24rpx;">-￥200</label>
			</view>
			<view hover-class="touchOpacity">
				<label for="" class="resDetail_title">优惠券</label>
				<label for="" style="margin-right:40rpx;color:#666666;font-size: 24rpx;">更多优惠券 ></label>
			</view>
		</view>
		<!-- 确认订单按钮 -->
		<view class="foot">
			<view class="foot_left">
				<text class="foot_left_count" space="ensp">合计<text>￥{{countmoney}}</text></text>
				<text class="foot_left_ticket" space="ensp">已免运费   <text>已优惠￥100</text></text>
			</view>
			<view class="foot_right" hover-class="touchOpacity" @click="goPay">确认订单</view>
		</view>
	
	</view>
</template>

<script>

	export default {
		data() {
			return {
				orders:[],
				countmoney:0,
				countnumber:0,
				address:{},//初始化收货地址
				localBuffer:true,//初始化收货地址开关
			}
		},
		onLoad(option){
			let carts = uni.getStorageSync('carts')
			this.orders = carts.carts
			this.countmoney = carts.totalPrice
			this.countnumber = carts.totalNum
			
			let address = uni.getStorageSync('address')
			if(address){
				this.address = address
			}else{
				this.getAdrress()
			}
		},
		methods: {
			// 去支付页面
			async goPay(){
				// 1-先拿到购物车中商品存储的id，根据id去购物车查找，如果购物车没有,证明已经生成订单，不能重复生成
				// 2-可以把第一次生成的订单号缓存
				let {token,uid} = uni.getStorageSync('userinfo')
				let {countmoney,countnumber} = this
				let addressid = this.address.id
				// console.log(token,uid,countmoney,countnumber,addressid);
				let idstr = this.orders.map((item)=>{
					return item.id
				})
				idstr = idstr.join(',')
				// console.log(idstr);
				let options = {
					url:'/api/orderadd',
					method:'POST',
					data:{
						uid,countmoney,countnumber,addressid,idstr
					},
					header:{
						authorization:token
					}
				}
				let res = await this.$http(options)
				if(res.data.code == 200){
					uni.navigateTo({
						url:'../pay/pay?outTradeNo=' + res.data.list.outTradeNo + '&countmoney=' + countmoney
					})
				}else{
					console.log(res);
				}
			},
			// 用户不适用默认地址，点击地址调到收货地址列表
			goAddress(){
				uni.navigateTo({
					url:'../address/address'
				})
			},
			// 获取收货地址列表
			async getAdrress(){
				let {uid,token} = this.$store.state.userinfo
				let options = {
					url:'/api/addresslist',
					data:{uid},
					header:{
						authorization:token
					}
				}
				let res = await this.$http(options)
				if(res.data.code == 200){
					if(res.data.list==null){  // 请求成功了，但是用户之前没有添加过收货地址
						this.localBuffer = false
						return
					}
					this.localBuffer = true
					res.data.list = res.data.list.filter((item)=>{
						return item.status == 1 ?true:false
					})
					// console.log(res.data.list);
					this.address = res.data.list[0]
				}else if(res.data.code == 403){
					uni.showToast({
						title:'请重新登录',
						icon:'error'
					})
					setTimeout(()=>{
						uni.navigateTo({
							url:'../login/login'
						})
					},2000)
				}else{
					uni.showToast({
						title:'请求失败',
						icon:'error'
					})
				}
			},
			addressAdd(){
				uni.navigateTo({
					url:'../address/addSite'
				})
			}
		}
	}
</script>

<style scoped>
	/* 引入样式 */
	@import url("@/static/css/order.css");
</style>
